from openai import OpenAI
import json
import requests

def add(a: int, b: int):
    return a + b


def mul(a: int, b: int):
    return a * b


tools1 = [{
    'type': 'function',
    'function': {
        'name': 'add',
        'description': 'Compute the sum of two numbers',
        'parameters': {
            'type': 'object',
            'properties': {
                'a': {
                    'type': 'int',
                    'description': 'A number',
                },
                'b': {
                    'type': 'int',
                    'description': 'A number',
                },
            },
            'required': ['a', 'b'],
        },
    }
}, {
    'type': 'function',
    'function': {
        'name': 'mul',
        'description': 'Calculate the product of two numbers',
        'parameters': {
            'type': 'object',
            'properties': {
                'a': {
                    'type': 'int',
                    'description': 'A number',
                },
                'b': {
                    'type': 'int',
                    'description': 'A number',
                },
            },
            'required': ['a', 'b'],
        },
    }
}]

def get_current_temperature(location: str):
    """获取地点当前的温度.

    Args:
        location: 获取温度的地点".

    Returns:
        温度 地点 in a dict
    """
    return {
        "temperature": 26.1,
        "location": location,
    }


def get_temperature_date(location: str, date: str):
    """获取地点和日期的温度.

    Args:
        location: 要获取其温度的位置".
        date: 要获取其温度的日期.

    Returns:
        the temperature, the location, the date and the unit in a dict
    """
    return {
        "temperature": 25.9,
        "location": location,
        "date": date,
    }

def get_function_by_name(name):
    if name == "get_current_temperature":
        return get_current_temperature
    if name == "get_temperature_date":
        return get_temperature_date

tools = [{
    'type': 'function',
    'function': {
        'name': 'get_current_temperature',
        'description': '获取地点当前的温度.',
        'parameters': {
            'type': 'object',
            'properties': {
                'location': {
                    'type': 'string',
                    'description': '要获取其温度的位置.'
                },
            },
            'required': [
                'location'
            ]
        }
    }
}, {
    'type': 'function',
    'function': {
        'name': 'get_temperature_date',
        'description': '获取某个地点某个日期的温度.',
        'parameters': {
            'type': 'object',
            'properties': {
                'location': {
                    'type': 'string',
                    'description': '要获取其温度的地点.'
                },
                'date': {
                    'type': 'string',
                    'description': '要获取其温度的日期.'
                },
            },
            'required': [
                'location',
                'date'
            ]
        }
    }
}]
messages1 = [{'role': 'user', 'content': '计算(3+5)*2'}]
messages = [{'role': 'user', 'content': '。2024-11-14, 中山市的天气如何'}]
API_KEY = 'V1RLSi1MTE0tU0VSVkVSLUJMQGxoeTIwMjQxMDIyCg'
BASE_API_URL = 'http://127.0.0.1:23333/v1'
API_URL = 'http://127.0.0.1:23333/v1/chat/completions'

client = OpenAI(api_key=API_KEY, base_url=BASE_API_URL)
print("models: ", client.models.list())
model_name = client.models.list().data[0].id
response = client.chat.completions.create(
    model=model_name,
    messages=messages,
    temperature=0.8,
    top_p=0.8,
    stream=False,
    tools=tools
)
print("message", response.choices[0].message)
messages.append(response.choices[0].message)

for tool_call in response.choices[0].message.tool_calls:
    tool_call_args = json.loads(tool_call.function.arguments)
    tool_call_result =  get_function_by_name(tool_call.function.name)(**tool_call_args)
    messages.append({
        'role': 'tool',
        'name': tool_call.function.name,
        'content': tool_call_result,
        'tool_call_id': tool_call.id
    })

response = client.chat.completions.create(
    model=model_name,
    messages=messages,
    temperature=0.8,
    top_p=0.8,
    stream=False,
    tools=tools)
print("message-func", response.choices[0].message.content)
'''
def call_chat_response(requests, prompt):
    try:
        headers = {
            'Content-Type': 'application/json',
            'Authorization': f'Bearer {API_KEY}'
        }
        data = {
            "model": "Qwen2.5-14B-Instruct-GPTQ-Int4",  			
            "messages": [{"role": "system", "content": "你是优秀的中文助手."}, {"role": "user", "content": prompt}]
        }
        
        response = requests.post(API_URL, json=data, headers=headers)
        if response.status_code == 200:
            result = response.json()
            #return result['choices'][0]['message']['content']
            return result
        else:
            print(f"Error: {response.status_code} - {response.text()}")
            return None
    except Exception as e:
        print(e)
        return None
'''